//
//  SPReasonDescView.m
//  panzer
//
//  Created by 还带大道 on 2018/10/22.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPReasonDescView.h"
#import "SPCommon.h"
#import "XLPhotoBrowser.h"
#import "UIButton+WebCache.h"

@interface SPReasonDescView()<XLPhotoBrowserDatasource>
@property (weak, nonatomic) IBOutlet UILabel *desc;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photosViewH;
@property (weak, nonatomic) IBOutlet UIView *photosView;

@property (strong, nonatomic) NSMutableArray<UIButton *> *imageVs;
@property (weak, nonatomic) IBOutlet UILabel *imageCount;
@property (weak, nonatomic) IBOutlet UIView *promptView;

@property (strong, nonatomic) NSArray *images;
@end

@implementation SPReasonDescView

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self addPhotos];
    _desc.text = @"商品直接就是坏的啊，用不了，麻烦给我换一个， 等快递就等了那么多天，到了还是个坏的。商品直接就是坏的啊，用不了，麻烦给我换一个， 等快递就等了那么多天，到了还是个坏的。";
    self.photosView.backgroundColor = [UIColor whiteColor];
    
}


- (void)addPhotos
{
     _images = @[@"http://pic.58pic.com/58pic/14/20/58/73z58PIC5MR_1024.jpg",
                 @"http://pic21.photophoto.cn/20111106/0020032891433708_b.jpg",
                 @"http://attach.bbs.miui.com/forum/201303/16/173716jzszx8vbbb0z9o4k.jpg",
                 @"http://imgsrc.baidu.com/imgad/pic/item/34fae6cd7b899e51fab3e9c048a7d933c8950d21.jpg"];
    
    _imageVs = [NSMutableArray array];
    NSInteger count = _images.count>3?3:_images.count;
    for (NSInteger index =0; index<count; index++) {
        UIButton *btn = [UIButton new];
        btn.tag = index;
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn sd_setImageWithURL:[NSURL URLWithString:_images[index]] forState:UIControlStateNormal];
        [self.photosView addSubview:btn];
        [_imageVs addObject:btn];
    }
    _promptView.hidden = (_images.count<=3);
}



- (void)btnOnClick:(UIButton *)btn
{
    RZLog(@"----%ld",btn.tag);
   
    //基本样式（不可保存）
    [XLPhotoBrowser showPhotoBrowserWithImages:_images currentImageIndex:btn.tag];
    
    //简单样式（可保存）
//    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:btn.tag imageCount:images.count datasource:self];
//    browser.browserStyle = XLPhotoBrowserStyleSimple;
}


#pragma mark--XLPhotoBrowserDatasource
//- (UIImage *)photoBrowser:(XLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
//{
//    NSArray *images = @[[UIImage imageNamed:@"icon_live_pic"],[UIImage imageNamed:@"icon_live_pic"],[UIImage imageNamed:@"icon_live_pic"]];
//    return images[index];
//}
//
//- (UIView *)photoBrowser:(XLPhotoBrowser *)browser sourceImageViewForIndex:(NSInteger)index
//{
//    return self.imageVs[index].imageView;
//}



- (void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger maxCount = 3;
    CGFloat wh = 70;
    CGFloat margin = 10;
    __block CGFloat x = 0;
    __block CGFloat y = 0;
    __block NSInteger row = 0;
    __block NSInteger col = 0;
    
    [_imageVs enumerateObjectsUsingBlock:^(UIButton * _Nonnull btn, NSUInteger idx, BOOL * _Nonnull stop) {
        
        row = idx / maxCount;
        col = idx % maxCount;
        x = (wh+margin)*col;
        y = (wh+margin)*row;
        
        btn.frame = CGRectMake(x, y, wh, wh);
    }];
    
    _photosViewH.constant = CGRectGetMaxY(_imageVs.lastObject.frame);
}


@end
